<?php
declare(strict_types=1);

namespace App\Utils\Other\Payment\Sumpay;
use App\Exception\InternalException;

/**
 * 支付单报关
 * @package App\Utils\Other\Payment\Sandpay
 */
Class Declares extends App {

    /**
     * 推送支付单
     * @param array $params
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function push(array $params): array
    {
        $this->setConfig([
            'encrypt_fields' => [
                'payer_name', 'payer_number', 'bank_no', 'payer_phone_number'
            ]
        ]);

        $params['service'] = 'fosun.sumpay.api.crossborder.eport.declare.apply';
        $params['mer_no'] = $this->getConfig('mer_no');
        $params['eport_code'] = $this->getConfig('eport_code');
        $params['eport_company_code'] = $this->getConfig('eport_company_code');
        $params['eport_company_name'] = $this->getConfig('eport_company_name');

        [$params, $response, $body] = $this->request($params);

        $body = json_decode($body['eport_apply_response'], true);
        if ($body['chk_mark'] == 3) {
            throw new InternalException(json_encode($response, JSON_UNESCAPED_UNICODE));
        }

        return [$params, $response, $body, $this->getConfig('url')];
    }

    /**
     * 查询支付单
     * @param array $params
     * @return array
     */
    public function query(array $params): array
    {
        $params['service'] = 'fosun.sumpay.api.crossborder.eport.declare.query';
        $params['mer_no'] = $this->getConfig('mer_no');

        return $this->request($params);
    }
}